<?php

class ProductsController extends Controller
{
	public $_model;
        public $layout;


	public function filters()
	{
		return array(
			'accessControl',
		);
	}	

	public function accessRules() {
		return array(
				);
	}

//	public function beforeAction($action) {
//		$this->layout = Shop::module()->layout;
//		return parent::beforeAction($action);
//	}

	public function actionView()
	{       
                $a = Yii::app()->getRequest()->getParam('id');
                //echo $a;
                $suppliers = new CActiveDataProvider('DistributorProduct', array(
                    'criteria' => array(
                        //'select'=>'distributor_id',
                        'condition' => 'product_id = ' .$a
                    )
                ));
                $suppliers->pagination->pageSize = 5;
                //var_dump($suppliers);
		$this->render('view',array(
			'model'=>$this->loadModel(),
                        'data'=>$suppliers,
                        'hasDis'=>DistributorProduct::model()->count('product_id = ' .$a)
		));
	}

	/**
	 * Lists all models.
	 */
	public function actionIndex()
	{
		$dataProvider = new CActiveDataProvider('Product',
                        array(
                            'criteria' => array(
                        'order' => 'RAND() '
                    )
                        )
                        );
                $dataProvider->pagination->pageSize = 5;

		$this->render('index',array(
			'dataProvider'=>$dataProvider,
		));
	}

	/**
	 * Returns the data model based on the primary key given in the GET variable.
	 * If the data model is not found, an HTTP exception will be raised.
	 */
	public function loadModel()
	{
		if($this->_model===null)
		{
			if(isset($_GET['id']))
				$this->_model=Product::model()->findbyPk($_GET['id']);
			if($this->_model===null)
				throw new CHttpException(404,'The requested page does not exist.');
		}
		return $this->_model;
	}

	/**
	 * Performs the AJAX validation.
	 * @param CModel the model to be validated
	 */
	protected function performAjaxValidation($model)
	{
		if(isset($_POST['ajax']) && $_POST['ajax']==='Product-form')
		{
			echo CActiveForm::validate($model);
			Yii::app()->end();
		}
	}
}
